#include "compile_run.hpp"
#include "../comm/httplib.h"
using namespace ns_compile_and_run;
using namespace httplib;

void Usage(string proc)
{
    std::cerr<<"Usage: " <<"\n\t"<<proc<<" port"<<std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return 1;
    }

    Server svr;

    svr.Get("/hello",[](const Request&req,Response& resp){
        // 基本测试
        resp.set_content("hello httplib,你好 httplib!","text/plain;charset=utf-8");
    });

    svr.Post("/compile_and_run",[](const Request& req ,Response& resp){
        // 用户请求的Request正文正好就是我们想要的json string
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        }
    });
    
    svr.listen("0.0.0.0",atoi(argv[1])); //启动http服务










    // // in_json : {"code" : "#include ....", "input" : "....", "cpu_limit" : 1, "mem_limit" : "10240"};
    // // out_json：{"status" : "0", "reason" : "...", "stdout" : "...", "stderr" : "..."} ;

    // // 测试-------------》》》》》》》》》》》充当客户端请求过来的json串
    // std::string in_json;

    // Json::Value in_value;
    // in_value["code"] = R"(#include <iostream>
    // int main()
    // {
    //     std::cout<<"hello code"<<std::endl;
    //     int a = 10;
    //     return 0;
    // })";
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 1024 * 30;

    // Json::FastWriter write; 
    // in_json = write.write(in_value);
    // std::cout<<in_json<<std::endl;

    // string out_value;
    // CompileAndRun::Start(in_json,&out_value);
    // std::cout<<out_value<<std::endl;
    // return 0;
}